package tech.aistar.prj.controller;

import tech.aistar.prj.entity.Cust;
import tech.aistar.prj.service.ICustService;
import tech.aistar.prj.service.Impl.CustImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 本类用于：
 *
 * @author 许华钢
 * @date 2020/9/26 19:56
 */
@WebServlet(urlPatterns = "/customer/*")
public class CustHandlerAction extends HttpServlet {
    private ICustService custService = new CustImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String pathInfo = req.getPathInfo();

        switch (pathInfo){
            case "/index":
                List<Cust> custs = custService.findAll();

                req.setAttribute("cusus",custs);
                req.getRequestDispatcher("/jsp/index.jsp").forward(req,resp);
                break;
            case "/del":
                String cid = req.getParameter("id");
                custService.del(Integer.valueOf(cid));
                resp.sendRedirect("/cust/customer/index");
                break;
            case "/searchCont":
                String cstatus = req.getParameter("status_id");
                String cont = req.getParameter("cont");
                List<Cust> custList =custService.searchCont(cont,Integer.valueOf(cstatus));
                req.setAttribute("cusus",custList);
                req.getRequestDispatcher("/jsp/index.jsp").forward(req,resp);
                break;
            case "/creatCust":
                String id = req.getParameter("id");
                String cname = req.getParameter("cname");
                String cpwd = req.getParameter("cpwd");
                String cstat = req.getParameter("stat");


                Cust cust = new Cust();
                cust.setId(Integer.valueOf(id));
                cust.setCname(cname);
                cust.setCpassword(cpwd);
                cust.setCstatus(Integer.valueOf(cstat));

                custService.insertCust(cust);
                resp.sendRedirect("/cust/customer/index");
                break;
            case "/update":
                String up_id = req.getParameter("id");
                String up_cname = req.getParameter("cname");
                String up_cpwd = req.getParameter("cpwd");
                String up_cstat = req.getParameter("stat");

                Cust up_cust = new Cust();
                up_cust.setId(Integer.valueOf(up_id));
                up_cust.setCname(up_cname);
                up_cust.setCpassword(up_cpwd);
                up_cust.setCstatus(Integer.valueOf(up_cstat));

                custService.updateByCust(up_cust);
                resp.sendRedirect("/cust/customer/index");
                break;
        }
    }
}
